/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo;

import java.awt.Component;
import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.undo.AbstractUndoableEdit;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;
import javax.swing.undo.UndoableEditSupport;

import twaver.DataBoxSelectionAdapter;
import twaver.DataBoxSelectionEvent;
import twaver.Element;
import twaver.TDataBox;
import twaver.TWaverUtil;
import twaver.tree.TTree;

public class NavigateManager {
	
	class UndoableEdit extends AbstractUndoableEdit {
		private Element element;
		private Element saveElement;
		
		public UndoableEdit() {
			this.element = lastSelectedElement;
		}

		public void redo() throws CannotRedoException {
			super.redo();
			isAdjusting = true;
			box.getSelectionModel().setSelection(saveElement);
			isAdjusting = false;
		}

		public void undo() throws CannotUndoException {
			super.undo();
			isAdjusting = true;
			saveElement = lastSelectedElement;
			box.getSelectionModel().setSelection(element);
			isAdjusting = false;
		}
	}
	
	private TDataBox box;
	private JSplitPane rootSplit;
	private JScrollPane scrollReadMe;
	private boolean isAdjusting = false;
	private Element lastSelectedElement = null;
	private UndoManager manager = new UndoManager();
	private UndoableEditSupport undoableEditSupport = new UndoableEditSupport();
	private JButton backButton = new JButton(TWaverUtil.getImageIcon("/resource/image/direction/w.png"));
	private JButton forwardButton = new JButton(TWaverUtil.getImageIcon("/resource/image/direction/e.png"));
	private JButton fullButton = new JButton(TWaverUtil.getImageIcon("/demo/resource/images/toggle.gif"));
	
	public NavigateManager(final TTree tree, final JSplitPane rootSplit, JScrollPane scrollReadMe){
		this.box = tree.getDataBox();
		this.rootSplit = rootSplit;
		this.scrollReadMe = scrollReadMe;
		
		backButton.setFocusPainted(false);
		forwardButton.setFocusPainted(false);
        fullButton.setFocusPainted(false);
		
		fullButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				Component compnent = rootSplit.getRightComponent();
				if(compnent instanceof DemoPane){
					DemoPane demoPane = (DemoPane)compnent;
					demoPane.getFullScreenSupport().showFullScreen();
				}
			}
		});
        backButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				manager.undo();
			}
        });
        forwardButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				manager.redo();
			}
        });
        undoableEditSupport.addUndoableEditListener(manager);
        
        box.getSelectionModel().addDataBoxSelectionListener(new DataBoxSelectionAdapter() {
            public void selectionChanged(DataBoxSelectionEvent dataBoxSelectionEvent) {
            	Cursor cursor = tree.getCursor();
            	tree.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
            	selectionChangedProcess();
                tree.setCursor(cursor);
            }
        });
        
        this.updateButton();
	}
	
	public JButton getBackButton() {
		return backButton;
	}

	public JButton getForwardButton() {
		return forwardButton;
	}

	public JButton getFullButton() {
		return fullButton;
	}
	
	private void updateButton(){
        this.backButton.setEnabled(manager.canUndo());
        this.forwardButton.setEnabled(manager.canRedo());
        this.fullButton.setEnabled(rootSplit.getRightComponent() != scrollReadMe);
	}
	
    public void selectionChangedProcess(){
    	int location = rootSplit.getDividerLocation();
        if (box.getSelectionModel().size() == 1) {
            if (box.getLastSelectedElement() instanceof DemoNode) {
                DemoNode node = (DemoNode) box.getLastSelectedElement();
                if (node.getDemoPane() == null) {
                    Class clazz = node.getDemoClass();
                    try {
                        node.setDemoPane( (DemoPane) clazz.newInstance());
                    }catch (Exception e) {
                    	e.printStackTrace();
                    }
                }
                rootSplit.setRightComponent(node.getDemoPane());
                if(!isAdjusting && lastSelectedElement != null){
                	undoableEditSupport.postEdit(new UndoableEdit());
                }
                lastSelectedElement = node;
            }
        } else {
        	rootSplit.setRightComponent(scrollReadMe);
        }
        this.rootSplit.setDividerLocation(location);
        this.updateButton();
    }


}